package com.gusha.app;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.dao.FarmOperationDao;
import com.gusha.dao.SysAssetsDao;
import com.gusha.dao.SysScenicDao;
import com.gusha.model.FarmOperation;
import com.gusha.model.SysScenic;
import com.gusha.model.User;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

@CrossOrigin(origins="*",maxAge=3600)
@Controller
@RequestMapping("/app/farmOperation")
public class AppFarmOperationController {
	@Autowired
	FarmOperationDao farmOperationDao;
	@Autowired
	SysAssetsDao sysAssetsDao;
	@Autowired
	SysScenicDao sysScenicDao;
	
	
	
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toWatering")
	@ResponseBody
	public JsonResult toWatering(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		
		String type = WebValueUtil.getWebValue(request, "type");
		String user = WebValueUtil.getWebValue(request, "user");
		String opType = WebValueUtil.getWebValue(request, "opType");
	if(StringUtils.isNotBlank(type)&&StringUtils.isNotBlank(user)) {
		FarmOperation f=new FarmOperation();
		User userId=new User();
		userId.setId(new BigInteger(user));
		f.setUserId(userId);
		f.setShopType(new BigInteger(type));
		f.setOpType(new BigInteger(opType));
		Date date=new Date();
		f.setOpTime(date);
		farmOperationDao.saveAndFlush(f);
		result.setMsg("操作成功");
		result.setFlag(true);
		System.out.println("用户"+user+"对他的"+type+"进行了一次"+opType);
		return result;
	}
		return result;
	}
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toStrategyList")
	@ResponseBody
	public JsonResult toStrategyList() {
		JsonResult result=new JsonResult(false);
		List<SysScenic> list1=sysScenicDao.findAllByScenicType(new BigInteger("1"));
		List<SysScenic> list2=sysScenicDao.findAllByScenicType(new BigInteger("2"));
		List<SysScenic> list3=sysScenicDao.findAllByScenicType(new BigInteger("3"));
		Map<String,Object> map=new HashMap<>();
		map.put("xuexi", list1);
		map.put("xiuxicanyin", list2);
		map.put("youwanyule", list3);
		result.setMsg("操作成功");
		result.setObj(map);
		result.setFlag(true);
		return result;
	
	}
	
	
	@CrossOrigin(origins="*",maxAge=3600)
	@RequestMapping("/toStrategy")
	@ResponseBody
	public JsonResult toStrategy(Model model,HttpServletRequest request) {
		JsonResult result=new JsonResult(false);
		String id = WebValueUtil.getWebValue(request, "id");
		if(StringUtils.isNotBlank(id)) {
		SysScenic scenic=sysScenicDao.findAllById(new BigInteger(id));
		result.setMsg("操作成功");
		result.setObj(scenic);
		result.setFlag(true);
		return result;
		}
		
		return result;
	
	}
	
}
